import { routerRedux } from 'dva/router';
import { stringify } from 'qs';
import { fakeAccountLogin, getFakeCaptcha,merchantsLogin } from '@/service/api';
import { setAuthority, getAuthority } from '@/utils/authority';
import { getPageQuery } from '@/utils/utils';
import { reloadAuthorized } from '@/utils/Authorized';
import { message } from 'antd';

export default {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
    * accountLogin({ payload }, { call, put }) {
      const response = yield call(fakeAccountLogin, payload);
      // 登录成功
      console.log(response);
      if(response===undefined){
        message.error('网络错误～');
      }
      if (response.code === 200) {
        response.msg.status = true;
        setAuthority(response.msg);
        if (getAuthority('antd-pro-authority')) {
          yield put(routerRedux.replace(response.msg.menus[0].children[0].path));
        }
      } else {
        message.error(response.errMsg);

      }
    },

    * merchantsLogin({ payload }, { call, put }) {
      const response = yield call(merchantsLogin, payload);
      // 登录成功
      console.log(response);
      if(response===undefined){
        message.error('网络错误～');
      }
      if (response.code === 200) {
        response.msg.status = true;
        setAuthority(response.msg);
        if (getAuthority('antd-pro-authority')) {
          yield put(routerRedux.replace(response.msg.menus[0].children[0].path));
        }
      } else {
        message.error(response.errMsg);

      }
    },

    * getCaptcha({ payload }, { call }) {
      yield call(getFakeCaptcha, payload);
    },

    * logout(_, { put }) {
      // yield put({
      //   type: 'changeLoginStatus',
      //   payload: {
      //     status: false,
      //     currentAuthority: 'guest',
      //   },
      // });
      // reloadAuthorized();
      yield put(
        routerRedux.push({
          pathname: '/user/login',
        }));
      localStorage.clear();

    },
  },

  reducers: {
    changeLoginStatus(state, { payload }) {
      return {
        status: payload,
      };
    },
  },
};
